package com.gdth.bank.adapter;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.database.bean.TAppBfrjZcxxb;
import com.gdth.model.Item;
import com.gdth.utils.DateTimeUtil;

/**
 * 资产信息
 * @author arvin
 *
 */
public class CjkhZcxxListViewAdapter extends BaseAdapter {
	private Context context;

	private List<TAppBfrjZcxxb> vList;
	
	private int layout;
	
	public CjkhZcxxListViewAdapter(Context context, List<TAppBfrjZcxxb> vList) {  
	    this.context = context;
	    this.vList = vList;
	    
	    this.layout = R.layout.item_cjkh_zcxx;
	}
	
	@Override 
	public int getCount() {  
	    return vList.size();  
	}  
	
	@Override 
	public Object getItem(int position) {  
	    return position;  
	}  
	
	@Override 
	public long getItemId(int position) {  
	    return position;  
	}
	
	@Override 
	public View getView(final int position, View convertView, ViewGroup parent) { 
		TAppBfrjZcxxb model = vList.get(position);
		ViewHolder holder = null;
		if (convertView == null){
			holder = new ViewHolder();
			
			convertView = LayoutInflater.from(context).inflate(layout, null);
			
		    holder.local_zczl = (TextView) convertView.findViewById(R.id.cjkh_zcxx_item_zczl);
		    holder.local_zcsyz = (TextView) convertView.findViewById(R.id.cjkh_zcxx_item_zcsyz);
		    holder.local_zcje = (TextView) convertView.findViewById(R.id.cjkh_zcxx_item_zcje);
			holder.local_delete = (TextView) convertView.findViewById(R.id.cjkh_zcxx_item_delete);

			convertView.setTag(holder);
	    } else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (holder != null){
			if (items != null) {
				for (Item item : items) {
					if (item.getKey().equals(model.getZczl().toString()))
						holder.local_zczl.setText(item.getValue());
				}
			}
			holder.local_zcsyz.setText(model.getZcsyr());
			String je = new DecimalFormat("###,###,###.##").format(model.getZcje());
			holder.local_zcje.setText(je);
			Date date = model.getCjsj();
//			if (mOnDeleteClickListener != null && DateTimeUtil.isToday(date)) {
			if (mOnDeleteClickListener != null ) {
				holder.local_delete.setVisibility(View.VISIBLE);
				holder.local_delete.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						mOnDeleteClickListener.delete(position);
					}
					
				});
			} else {
				holder.local_delete.setVisibility(View.GONE);
			}
		}
		
	    return convertView;  
	}
	
	public final class ViewHolder{
		public TextView local_zczl = null;
		public TextView local_zcsyz = null;
		public TextView local_zcje = null;
		public TextView local_delete = null;
	}
	
	private List<Item> items;
	
	public void setItems(List<Item> items) {
		this.items = items;
	}
	
	private OnDeleteClickListener mOnDeleteClickListener;
	
	public void setOnDeleteClickListener(OnDeleteClickListener mOnDeleteClickListener) {
		this.mOnDeleteClickListener = mOnDeleteClickListener;
	}
	
	public interface OnDeleteClickListener {
		public void delete(int position);
	};
}
